package com.person.monitor;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ProcessListAdapter extends BaseAdapter {

    Context context;

    List<BaseProcess> processList = new ArrayList<>();
    LayoutInflater layoutInflater;

    public ProcessListAdapter(Context context){
        this.context = context;

        layoutInflater = LayoutInflater.from(context);
    }

    public void setProcessList(List<BaseProcess> list){
        processList = MonitorServer.processList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return processList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.list_item_process,parent,false);
        }

        TextView logText = convertView.findViewById(R.id.list_item_log_text);

        logText.setText(processList.get(position).getProcessName());

        CheckBox checkBox = convertView.findViewById(R.id.list_item_check);

        checkBox.setChecked(processList.get(position).getProcessEnable());

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                processList.get(position).setProcessEnable(isChecked);
            }
        });

        return convertView;
    }
}
